<template>
  <el-dialog v-model="visible" :title="`${!dataForm.id ? $t('add') : $t('update')}设备信息`" :close-on-click-modal="false"
    :close-on-press-escape="false" width="900">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="100px">
      <TitleBar title="基础字段填写" />
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceName')" prop="deviceName" required>
            <el-input v-model="dataForm.deviceName" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceCode')" prop="deviceCode" required>
            <el-input v-model="dataForm.deviceCode" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceModel')" prop="deviceModel">
            <el-input v-model="dataForm.deviceModel" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceType')" prop="deviceType" required>
            <el-select v-model="dataForm.deviceType" placeholder="请选择">
              <el-option v-for="item in DEVICE_TYPE_OPTIONS" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceManufacturer')" prop="deviceManufacturer">
            <el-select v-model="dataForm.deviceManufacturer" placeholder="请选择">
              <el-option v-for="item in DEVICE_MANUFACTURER_OPTIONS" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('device.groupId')" prop="groupId">
            <el-cascader v-model="dataForm.groupId" :options="deviceGroupList" :props="groupProps" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceLocation')" prop="deviceLocation">
            <el-input v-model="dataForm.deviceLocation" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('device.devicePurpose')" prop="devicePurpose">
            <el-select v-model="dataForm.devicePurpose" placeholder="请选择">
              <el-option v-for="item in DEVICE_PURPOSE_OPTIONS" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('device.deviceChannel')" prop="deviceChannel">
            <el-input v-model="dataForm.deviceChannel" type="number" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <TitleBar title="特殊字段填写" />
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('device.deviceAccount')" prop="deviceAccount" label-width="160px">
            <el-input v-model="dataForm.deviceAccount" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('device.devicePassword')" prop="devicePassword" label-width="160px">
            <el-input v-model="dataForm.devicePassword" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('device.deviceIp')" prop="deviceIp" label-width="160px">
            <el-input v-model="dataForm.deviceIp" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('device.devicePort')" prop="devicePort" label-width="160px">
            <el-input v-model="dataForm.devicePort" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import { DEVICE_MANUFACTURER_OPTIONS, DEVICE_TYPE_OPTIONS, DEVICE_PURPOSE_OPTIONS } from './contant';
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const groupProps = {
  checkStrictly: true,
  value: 'id',
  label: 'name',
  emitPath: false,
};

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  deviceName: "",
  deviceManufacturer: null,
  deviceType: null,
  groupId: null,
  deviceLocation: "",
  deviceModel: "",
  devicePurpose: null,
  deviceChannel: null,
  deviceCode: "",
  deviceAccount: "",
  devicePassword: "",
  deviceIp: "",
  devicePort: "",
  isSurvival: null,
});

const rules = ref({
  deviceName: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  deviceCode: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  deviceType: [{ required: true, message: t("validate.required"), trigger: "change" }],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getDeviceGroupList()]).then(() => {
    if (id) {
      getInfo(id);
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/device/info/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 获取编组列表
const deviceGroupList = ref<any[]>([]);
const getDeviceGroupList = () => {
  baseService.get("/device/class/list").then((res) => {
    deviceGroupList.value = res.data;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    baseService[!dataForm.id ? 'post' : 'put'](`/device/info/${!dataForm.id ? 'save' : ''}`, {
      ...dataForm,
      isSurvival: dataForm.isSurvival ? 1 : 0,
    }).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
